import React, {Component} from 'react';
import {Table, Tag, Space, message,Button} from 'antd';
import { NavLink } from 'react-router-dom';
import axios from "../../utils/http"
import './Mabeger.css'
import {getsessionstorage} from "../../utils/utils";
import {render} from "@testing-library/react";
const { Column, ColumnGroup } = Table;

const columns = [
    {
        title: 'Id',
        dataIndex: 'adminId',
        key: 'adminId',
    },
    {
        title: '姓名',
        dataIndex: 'adminUsername',
        key: 'adminUsername',
        render: text => <a>{text}</a>,
    },
    {
        title: '创建时间',
        dataIndex: 'adminLlt',
        key: 'adminLlt',
    },
    {
        title: '最后登录时间',
        dataIndex: 'adminCt',
        key: 'adminCt',
    },

    {
        title: '操作',
        key: 'action',

        render: (text, record) => (
            <Space size="middle">
                <Button onClick={()=>{
                    this.props.history.push(`/Home/quanXian/AssignPermissions/${text.adminId}`)
                }}>qweas</Button>
                {/*<NavLink to={{pathname:'/Home/quanXian/AssignPermissions',state:{name:text}}}>分配权限</NavLink>*/}
                <NavLink to={{pathname:'/Home/quanXian/SelectLog',state:{name:text}}}>查看日志</NavLink>
                <NavLink to={{pathname:'/Home/quanXian/ChangManeger',state:{name:text}}}>修改</NavLink>
                <Button onClick={()=>{
                    console.log()
                     axios.get('/mm-power//administrators/deleteAdministrators',{
                         params:{
                             adminId:text.adminId
                         }
                     }).then(data=>{
                         console.log(data)
                         if(data.data.code===200){
                             message.success('删除成功')
                             // this.setState()
                         }else{
                             message.error('删除失败')
                         }

                     })
                }}>删除</Button>

            </Space>
        ),
    },
];
export default class Maneger extends Component {
    constructor(props) {
        super(props);
        this.state = {
            center:'center',
            pagination: {
                defaultPageSize:5,
                defaultCurrent:1,
                total:13
            },
            data:'',
        };
    }

    componentDidMount=()=>{
        axios.get('/mm-power/administrators/queryAdministrators',{
            params:{
                adminId:getsessionstorage('id'),
            }
        }).then(data=>{
            function rTime(date) {
                var json_date = new Date(date).toJSON();
                return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
            }

            console.log(data)
            data.data.data.forEach(item=>{
             item.adminCt=rTime(item.adminCt)
                   item.adminLlt=rTime(item.adminLlt)
         })

            this.setState({
                    data:data.data.data,
                    pagination: {
                        defaultPageSize:5,
                        defaultCurrent:1,
                        total:data.data.data.length
                    },
                },
            )
        })
    }

    render() {
        const {center,pagination}=this.state

        return (
            <div className='Maneger'>
                <div className='biaoTi'>
                    <NavLink to='/Home/quanXian/Maneger'><span>管理</span></NavLink>
                    <NavLink to='/Home/quanXian/AddManeger'><span className='spanT'>添加管理员</span></NavLink>
                </div>

                <Table pagination={pagination}  dataSource={this.state.data} >

                    <Column title="adminId" dataIndex="adminId" key="adminId" />
                    <Column title="用户名" dataIndex="adminUsername" key="adminUsername" />
                    <Column title="创建时间" dataIndex="adminLlt" key="adminLlt" />
                    <Column title="最后登录时间" dataIndex="adminCt" key="adminCt" />
                    <Column
                        title="操作"
                        key="action"
                        render={(text, record) => (

                            <Space size="middle">

                                <NavLink to={`/Home/quanXian/AssignPermissions/${text.adminId}`}>分配权限</NavLink>
                                <NavLink to={`/Home/quanXian/SelectLog/${text.adminId}`}>查看日志</NavLink>
                                <NavLink to={`/Home/quanXian/ChangManeger/${text.adminId}`}>修改</NavLink>
                                {/*<NavLink to={{pathname:'/Home/quanXian/AssignPermissions',state:{name:text}}}>分配权限</NavLink>*/}
                                {/*<NavLink to={{pathname:'/Home/quanXian/SelectLog',state:{name:text}}}>查看日志</NavLink>*/}
                                {/*<NavLink to={{pathname:'/Home/quanXian/ChangManeger',state:{name:text}}}>修改</NavLink>*/}
                                <Button onClick={()=>{
                                    console.log(this)
                                    console.log(text.adminId)
                                    axios.get('/mm-power//administrators/deleteAdministrators',{
                                        params:{
                                            adminId:text.adminId
                                        }
                                    }).then(data=>{
                                        console.log(data)
                                        if(data.data.code===200){
                                            message.success('删除成功')
                                            // this.setState()
                                        }else{
                                            message.error('删除失败')
                                        }

                                    })
                                    axios.get('/mm-power/administrators/queryAdministrators',{
                                        params:{
                                            adminId:getsessionstorage('id'),
                                        }
                                    }).then(data=>{
                                        function rTime(date) {
                                            var json_date = new Date(date).toJSON();
                                            return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
                                        }
                                        data.data.data.forEach(item=>{
                                            item.adminCt=rTime(item.adminCt)
                                            item.adminLlt=rTime(item.adminLlt)
                                        })

                                        this.setState({
                                                data:data.data.data,
                                                pagination: {
                                                    defaultPageSize:5,
                                                    defaultCurrent:1,
                                                    total:data.data.data.length
                                                },
                                            },
                                        )
                                    })

                                }

                                }>删除</Button>

                            </Space>
                        )}
                    />
                </Table>

            </div>
        )
    }
}